package com.keymozy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.keymozy.pojo.Staff;
import com.keymozy.proxy.Right;
import com.keymozy.service.StaffService;
import com.keymozy.state.Code;
import com.keymozy.state.RespData;
import com.keymozy.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Date;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/staff")
@CrossOrigin
public class StaffController {
    @Autowired
    private StaffService staffService;

    @GetMapping
    public RespData getUserInfo(){
        Staff staff =(Staff) TokenUtil.data.get();
        return new RespData(Code.SUCCESS_SEARCH,staffService.getById(staff.getId()));
    }
    /**
     * 查询所有员工
     * @param currentPage
     * @param pageSize
     * @param staff
     * @return
     */
    @PostMapping("/getAll/{current}/{pageSize}")
    @Right(power = "2")
    public RespData getAll(@PathVariable("current") Integer currentPage,
                           @PathVariable("pageSize") Integer pageSize,@RequestBody(required = false) Staff staff){
        log.info("进入了staff.getAll");
        log.info("当前页："+currentPage);
        log.info("页大小:"+pageSize);
        log.info("staff:"+staff);
        log.info("当前使用者:"+ TokenUtil.data.get());
        return new RespData(Code.SUCCESS_STAFF_1,staffService.getAll(currentPage, pageSize, staff));
    }

    /**
     * 添加员工
     * @param staff
     * @return
     */
    @PostMapping("/add")
    @Right(power = "2")
    public RespData add(@RequestBody Staff staff){
        log.info("add:"+staff);
        if (!this.staffService.add(staff)){
            return new RespData(Code.FALSE_ADD,null);
        }
        return new RespData(Code.SUCCESS_ADD_1,null);
    }

    /**
     * 修改员工
     * @param staff
     * @return
     */
    @PostMapping("/alter")
    @Right(power = "2")
    public RespData alter(@RequestBody Staff staff){
        log.info("alter:"+staff);
        if (!this.staffService.alter(staff)){
            return new RespData(Code.FALSE_UPDATE,"修改员工信息成功");
        }
        return new RespData(Code.SUCCESS_UPDATE_1,"修改员工信息失败");
    }

    /**
     * 删除员工
     * @param staffs
     * @return
     */
    @PostMapping("/remove")
    @Right(power = "2")
    public RespData remove(@RequestBody Staff[] staffs){
        log.info("进入了删除staff");
        log.info("staffs"+staffs);
        if (this.staffService.delete(staffs)){
            return new RespData(Code.SUCCESS_DELETE_1,null);
        }
        return new RespData(Code.FALSE_DELETE,null);
    }

    /**
     * 根据职位查询员工
     * @param pid
     * @return
     */
    @GetMapping("/getByPid")
    public RespData getByPid(int pid){
        Staff staff = (Staff) TokenUtil.data.get();
        LambdaQueryWrapper<Staff> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Staff::getPid,staff.getPid());
        List<Staff> list = staffService.list(lqw);
        System.out.println(list);
        System.out.println(list.size());
        if (list.size()==0){
            throw new RuntimeException();
        }
        return new RespData(Code.SUCCESS_SEARCH,list);
    }
    @GetMapping("/echartstaff/{beginTime}/{endTime}")
    public RespData echartRigisterStaff(@PathVariable("beginTime") String beginTime,
                                        @PathVariable("endTime")String endTime){
    return new RespData(Code.SUCCESS_SEARCH,staffService.registerEchart(beginTime,endTime));
    }
}
